<?php

namespace app\api\controller;

use app\admin\model\forum\Itemdetail;
use app\api\model\Login as loginModel;
use app\common\controller\Api;
use app\common\model\ForumCategory;
use app\common\model\ForumItem;
use app\common\model\ForumItemDetail;
use app\common\model\SubjectTalk;
use think\Db;
use think\Request;
use app\common\model\User;

class Forum extends Api
{
    public function _initialize()
    {
        parent::_initialize();
        $this->loginModel = new loginModel();
        $this->number          = makeNumber();
        $this->beforeLog($this->number);
    }

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['getPageList', 'forumCategory'];

    /**
     * 帖子分类
     * forumCategory
     * user：Freak
     * createDate：2023/11/24 11:48
     */
    public function forumCategory()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'Forum.categoryList');
        if (true !== $result) $this->error($result, '', 0, $this->number);
        $where['status']   = 1;
        $forumCategoryList = ForumCategory::where($where)->select();
        foreach ($forumCategoryList as &$value) {
            $value['forumItemList'] = ForumItem::where('forum_category_id', $value['id'])->where($where)->select();
            foreach ($value['forumItemList'] as $item) {
                $item['forumItemDetailList'] = ForumItemDetail::where('forum_item_id', $item['id'])->where($where)->field('id as itemDetailId,title')->select();
            }
        }
        $this->success('', ['cateGoryList' => $forumCategoryList], '', $this->number);
    }


    /**
     * @return void
     * 发帖
     */
    public function postTalk()
    {
        $number = makeNumber();
        $params = $this->request->param();
        $result = $this->validate($params, 'Forum.postTalk');
        if (true !== $result) $this->error($result, '', 0, $number);
        $detailInfo = ForumItemDetail::get($params['detailId']);
        if (!$detailInfo || $detailInfo->status != 1) $this->error('细项详情记录有误', '', '', $number);
        $images = [];
        if (isset($params['images']) && !empty($params['images'])) {
            $images = json_encode($params['images']);
        }
        $images[] = ['url' => '/uploads/20231125/099825fe2c969d918c8413046bca5ace.jpg', 'type' => 'imageFile'];
        $images[] = ['url' => '/uploads/20231125/099825fe2c969d918c8413046bca5ace.jpg', 'type' => 'videoFile'];
        foreach ($images as $v) {
            if (!isset($v['url'])) $this->error('未上传文件');
            if (!isset($v['type'])) $this->error('未上传文件类型');
            if (!in_array($v['type'], ['imageFile', 'videoFile'])) {
                $this->error('上传类型错误');
            }
        }
        $images   = json_encode($images);
        $talkData = [
            'user_id'   => $this->auth->id,
            'detail_id' => $params['detailId'],
            'title'     => $params['title'],
            'content'   => $params['content'],
            'images'    => $images,
        ];
        $rs       = SubjectTalk::create($talkData);
        if ($rs) $this->success('发帖成功,等待审核通过');
        $this->error('发帖失败,请联系客服');
    }

    /**
     * @return void
     * 帖子列表
     */
    public function getPageList()
    {
        $number = makeNumber();
        $params = $this->request->param();
        $result = $this->validate($params, 'Forum.getPageList');
        if (true !== $result) $this->error($result, '', 0, $number);
        $detailInfo = ForumItemDetail::get($params['detailId']);
        if (!$detailInfo || $detailInfo->status != 1) $this->error('细项详情记录有误', '', '', $number);

        $order           = "id desc";
        $where['status'] = 2;
        if (isset($params['isNew']) && !empty($params['isNew'])) {
            $order = "create_time desc";
        }
        if (isset($params['isHeat']) && !empty($params['isHeat'])) {
            $order = "view_num desc";
        }
//        if(isset($params['isNewReply'])&& !empty($params['isNewReply'])){
//            $where['']
//        }
        $list = SubjectTalk::where($where)->order($order)
                           ->paginate($params['page'], false, ['page' => $params['pageCount']])
                           ->each(function ($item) {
                               $item['userInfo'] = User::where('id', $item['user_id'])->field('id,username,avatar')->find();
                               return $item;
                           });
        $this->success('帖子列表', $list, 1, $number);
    }
}